package org.lex.game.sudoku.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

import org.lex.game.sudoku.util.Formatter.DurationFormat;

public class TimeCountdownWidget extends JLabel implements ActionListener {
	private static final long serialVersionUID = 3583474229088849413L;

	private Timer countdownTimer = null;
	private long startTime = 0L;

	public TimeCountdownWidget() {
		super("00:00:00");
		this.setHorizontalAlignment(SwingConstants.CENTER);
		this.setFont(new Font(Font.DIALOG, Font.BOLD, 30));
	}

	public void start() {
		if (null == this.countdownTimer) {
			this.countdownTimer = new Timer(100, this);
		}
		this.startTime = System.currentTimeMillis();
		this.countdownTimer.start();
	}

	public void stop() {
		if (null != this.countdownTimer) {
			this.countdownTimer.stop();
		}
	}

	public void actionPerformed(ActionEvent e) {
		long duration = System.currentTimeMillis() - startTime;
		this.setText(DurationFormat.FULL.format(duration));
	}
}
